#include "read_json_db.h"

void clean_json_base64_string2( char *str )
{
	int i=0,j=0;
	ERROR("%s\n",str);
	while (str[i] != 0 )
	{
		if (str[i] == '\\')
		{
			PRINT("\n");
			j++;
		}
		i++;
		str[i-j]=str[i];

	}
	ERROR("%s\n",str);
}

void print_hex( uint8_t *d, int l, int s )
{
	int i = 0;
	while (i<l)
	{
		printf("%02x ", d[i]);
		if ((i+1)%s!=0) 
		{
			//printf("");
		} else 
		{
			printf("\n");
		}
		i++;
	}
}


char *new_string( const char *str )
{
	int str_s = strlen(str);
	char *new_str=MALLOC( str_s+1 );
	memcpy( new_str, str, str_s );
	if ( new_str != NULL )
		new_str[str_s]=0x0;
	return new_str;
}


//---------------------------------------------------------------------
db_db* open_json_db( const char *fname )
{

	struct json_object *jo=NULL;
	db_db *db = MALLOC( sizeof(db_db) );

	if ( db == NULL )
	{
		ERROR("Cannot open db\n");
		goto exit_error;
	}

	if ( fname == NULL )
	{
		ERROR("database file not defined\n");
		goto exit_error;
	}
	//check if file excists
	jo = json_object_from_file( fname );
	if ( jo == NULL )
	{
		ERROR("Cannot get json object from file %s\n", fname);
		goto exit_error;
	}

	db->jo = jo;
	db->db_file = new_string( fname );

	return db;

exit_error:
	if ( db != NULL )
	{
		free( db );
		db = NULL;
	}
	return db;
}


//---------------------------------------------------------------------
int close_json_db( db_db *db )
{
	if ( db != NULL )
	{
		json_object_put( db->jo );
		FREE( db->db_file );
		FREE( db );
		return 1;
	}
	return 0;
}


//---------------------------------------------------------------------
int get_user( db_db *db, const char *uname, db_user **du )
{
	struct json_object *jobj=NULL, *juser=NULL;
	db_user *user = MALLOC( sizeof(db_user) );

	if ( (db == NULL) || (uname == NULL) || (user == NULL))
	{
		ERROR("\n");
		return 0;
	}

	jobj = json_object_object_get( db->jo, "users" );
	if ( !json_object_is_type( jobj, json_type_object ) )
	{
		ERROR("\n");
		goto exit_error;
	}

	juser = json_object_object_get( jobj, uname );
	//
	struct json_object *juser_pass = json_object_object_get( juser, "password" );
	struct json_object *juser_seed = json_object_object_get( juser, "seed" );
	//struct json_object *user_lastlogin = json_object_object_get( juser, "lastlogin" );
	user->name = new_string( uname );
	char *str = new_string( json_object_to_json_string( juser_pass ) );
	str[strlen(str)-1] = 0x0;
	user->password = new_string( str+1 );
	clean_json_base64_string2( user->password );
	PRINT("%s\n", user->password);
	FREE( str );
	str = new_string( json_object_to_json_string( juser_seed ) );MALLOC(0);
	str[ strlen(str)-1 ] = 0x0;
	user->seed = new_string( str+1 );
	PRINT("%s\n",user->seed);
	clean_json_base64_string2( user->seed );
	FREE( str );

	*du = user;
	PRINT("\n");
	return 1;
	
exit_error:
	if (user != NULL)
	{
		FREE( user );
		user = NULL;
	}

	return 0;
}

int clean_user( db_user *du )
{
	FREE( du->name );
	FREE( du->password );
	FREE( du->seed );
	FREE( du );
	du = NULL;
	return 1;
}

int add_user( db_db *db, const char *name, const char *seed, const char *pass )
{
	//int ret;
	char *fname=NULL;
	struct json_object *jusers,*juser;

	db_user user; memset( &user, 0, sizeof(user) );

	if ( (db == NULL) || (name == NULL) || (pass == NULL) )
		return 0;
	
	//backup before manipulate data
	int tmp = strlen( db->db_file );
	fname = MALLOC( tmp + strlen(".bkp")+1 );
	memcpy( fname, db->db_file, tmp );
	memcpy( &fname[tmp], ".bkp", 4 );
	fname[tmp+4] = 0;
	
	json_object_to_file_ext( fname , db->jo, JSON_C_TO_STRING_PRETTY );
	FREE( fname );

	jusers = json_object_object_get( db->jo, "users" );
	juser = json_object_new_object();
	json_object_object_add( juser, "seed", json_object_new_string( seed ) );
	json_object_object_add( juser, "password", json_object_new_string( pass ) );
	json_object_object_add( juser, "lastlogin", json_object_new_string("none") );
	json_object_object_add( jusers, name, juser );
	json_object_to_file_ext( db->db_file, db->jo, JSON_C_TO_STRING_PRETTY  );

	return 1;
}

